package oj;

import java.util.Stack;

public class Num105 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String str = tokens[i];

            if (str == "+"|| str == "-" || str == "*" || str == "/") {
                int a = stack.pop();
                int b = stack.pop();
                int c =math(a,b,str);
                stack.push(c);
            }else {
                stack.push(Integer.parseInt(str));
            }
        }
        return stack.pop();
    }

    private int math(int a, int b, String str) {
        int result=0;
        switch (str){
            case "+":
                result=a+b;
                break;
            case "-":
                result=a-b;
                break;
            case "*":
                result=a*b;
                break;
            case "/":
                result=a/b;
                break;
        }
        return result;
    }
}